﻿using BaseUI;
using BLL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using Utility;

namespace Thksoft.Funds
{
    public partial class PlanWeekFunds1 : BasePage
    {
        #region 重构代码

        private QS_PLANWEEKAPPLYBLL applyWeekBll = new QS_PLANWEEKAPPLYBLL();

        #endregion 重构代码

        /// <summary>
        /// 判断是否首次加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["Ajax"] == "1")
                {
                    var jsonStr = "";
                    IResonResult result = null;
                    //读取JSON流
                    using (var stream = new System.IO.StreamReader(Request.InputStream))
                    {
                        jsonStr = stream.ReadToEnd();
                        if (string.IsNullOrEmpty(jsonStr))
                        {
                            return;
                        }
                    }
                    var jobj = JObject.Parse(jsonStr);
                    switch (jobj["Code"].ToObject<string>())
                    {
                        case "Find":
                            {
                                var jData = jobj["Data"];
                                int year = jData["Year"].ToObject<int>();
                                int month = jData["Month"].ToObject<int>();
                                int week = jData["Week"].ToObject<int>();
                                result = applyWeekBll.CreateOrFind((int)lognUser.USER_ID, (int)lognUser.USER_COMPANY, year, month, week);
                                break;
                            }
                        case "Get":
                            {
                                var jData = jobj["Data"];
                                int applyId = jData["ApplyId"].ToObject<int>();
                                var sql = @"SELECT
	T2.N_PROID AS PRO_ID,
	T2.N_PARENTID AS PRO_PID,
	NVL(T2.C_PRONAME,'') AS PRO_NAME,
	T1.N_AMOUNT AS AMOUNT,
	NVL(T1.C_REMARK,'') AS REMARK,
	T1.N_MONTHAMOUNT AS MONTH_AMOUNT,
	T1.N_SYAMOUNT AS  MONTH_USE_AMOUNT,
    T2.N_TYPE AS TYPE,
    T2.N_IF_DETAILS AS IF_DETAILS,
    T2.C_DETAILS_CODE AS DETAILS_CODE
FROM
	QS_PLANWEEK_DETAIL T1
INNER JOIN QS_PROJECT T2 ON T1.N_PROID = T2.N_PROID
WHERE
	T1.N_WEEKAPP_ID={0}
ORDER BY
	T2.N_PARENTID,
	T2.N_NUM1,
	T2.N_NUM2";
                                sql = string.Format(sql, applyId);
                                result = new ResonResult("获取成功", ResultType.success, applyWeekBll.Query(sql));
                                break;
                            }
                        case "Apply":
                            {
                                break;
                            }
                        case "Query":
                            {
                                try
                                {
                                    var jData = jobj["Data"];
                                    var sql = jData["Sql"].ToObject<string>();
                                    result = new ResonResult("执行成功", ResultType.success, new BaseBLL().Query(sql));
                                }
                                catch (Exception)
                                {
                                    result = new ResonResult("SQL异常");
                                }

                                break;
                            }
                        case "NoQuery":
                            {
                                try
                                {
                                    var jData = jobj["Data"];
                                    var sql = jData["Sql"].ToObject<string>();
                                    result = new ResonResult("执行成功", ResultType.success, new BaseBLL().NoQuery(sql));
                                }
                                catch (Exception)
                                {
                                    result = new ResonResult("SQL异常");
                                }
                                break;
                            }
                        default:
                            break;
                    }
                    if (result == null)
                    {
                        result = new ResonResult("没有找到接口");
                    }
                    Response.ContentType = "application/json";
                    Response.Write(JsonConvert.SerializeObject(result));
                    Response.Flush();
                    Response.End();
                    return;
                }
            }
        }
    }
}